#ifndef _XCC_H_
#define _XCC_H_
#include "cou.h"
#include<stdio.h>
#include<time.h>
#include<stdlib.h>

void print_wel(void) {
	printf("\n++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("\n++\t 欢迎使用学生选课管理系统\t  ++\n");
	printf("\n++++++++++++++++++++++++++++++++++++++++++++\n");
}

//录入课程子函数（从键盘读取）
void keyboard_cou(void) {
	struct course *p1,*p2;
	N1=0;
	char flag;
	p1=p2=(struct course*)malloc(sizeof(struct course));
	printf("\n请依次输入信息（以tab间隔）\n");
	printf("课程编号||课程名称||课程性质||授课学时||学分||课程人数上限\n");
	while(1) {
		scanf("%d\t%s\t%s\t%d\t%d\t%d",&p1->cou_num,p1->cou_name,p1->cou_type,&p1->hour,&p1->score,&p1->melepeo);
		p1->nelepeo=0;
		if(!N1)	head1=p1;
		p2->next = p1;
		FILE *fp1;
		time_t timep;
		struct tm *p;
		time(&timep);
		p=gmtime(&timep);
		fp1=fopen("log.log","a");
		fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t从键盘输入了课程号为%d的课程信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec,p1->cou_num);
		fclose(fp1);
		printf("\n录入成功，继续录入输入y，返回上一级请按任意键：");
		while ( (flag = getchar()) != '\n' && flag != EOF ) ; 
		scanf("%c",&flag);
		if(flag!='y'&&'Y') break;
		N1=N1+1;
		p2 = p1;
		p1 = (struct course*)malloc(sizeof(struct course));
	}
	p1->next=NULL;
	input_cou();
}

//录入课程子函数（从文件读取）
void file_cou(void) {
	FILE *fp;
	char filepath[50];
	struct course *p1, *p2;
	N1=0;
	printf("输入想要读入的文件路径：\n");
	getchar();
	gets(filepath);
	if((fp=fopen(filepath,"rb"))==NULL) {
		printf("找不到%s文件！\n",filepath);
		exit(0);
	}
	p1=p2=(struct course*)malloc(sizeof(struct course));
	fscanf(fp,"%d%s%s%d%d%d%d\n",&p1->cou_num,p1->cou_name,p1->cou_type,&p1->hour,&p1->score,&p1->nelepeo,&p1->melepeo);
	head1=NULL;
	while(!feof(fp)) {
		N1=N1+1;
		if(N1==1)
			head1=p1;
		else
			p2->next=p1;
		p2=p1;
		p1=(struct course*)malloc(sizeof(struct course));
		fscanf(fp,"%d%s%s%d%d%d%d",&p1->cou_num,p1->cou_name,p1->cou_type,&p1->hour,&p1->score,&p1->nelepeo,&p1->melepeo);
	}
	p2->next=NULL;
	FILE *fp1;
	time_t timep;
	struct tm *p;
	time(&timep);
	p=gmtime(&timep);
	fp1=fopen("log.log","a");
	fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t导入了class.txt文件中的课程信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec);
	fclose(fp1);
	system("cls");
	printf("\n录入成功!查看数据：\n");
	show_cou();
	input_cou();
}

//录入课程主函数
void input_cou(void) {
	int i;
	
	printf("\t录入课程信息\n");
	printf("\t1. 从键盘录入\n");
	printf("\t2. 从文件录入\n");
	printf("\t3. 浏览课程信息\n"); 
	printf("\t4. 返回上一级菜单\n");
	printf("\t请选择(1~4): ");
	scanf("%d",&i);
	switch(i) {
		case (1):
			keyboard_cou();
			break;
		case (2):
			file_cou();
			break;
		case (3):
			show_cou();
			break;
		case (4):
			manage_cou();
			break;
		default:
			printf("输入有误，请重新输入：\n\n");
			input_cou(); 
			break;
	}
}

//课程管理子函数（修改课程）
void insert_cou(int num2) {
	struct course *p1,*p2;
	if(head1==NULL) {
		printf("\n\t没有课程，无法修改！\n");
		goto end;
	}
	p1=head1;
	while(num2!=p1->cou_num && p1->next!=NULL) {
		p2=p1;
		p1=p1->next;
	}
	if(num2==p1->cou_num) {
		printf("\t请重新输入数据进行修改(用tab间隔)\n");
		printf("课程编号||课程名称||课程性质||授课学时||学分||课程已选人数||课程人数上限\n");
		scanf("%d\t%s\t%s\t%d\t%d\t%d\t%d",&p1->cou_num,p1->cou_name,p1->cou_type,&p1->hour,&p1->score,&p1->nelepeo,&p1->melepeo);
		printf("该课程修改成功！\n");
		FILE *fp1;
		time_t timep;
		struct tm *p;
		time(&timep);
		p=gmtime(&timep);
		fp1=fopen("log.log","a");
		fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t修改了一个课程号为%d课程信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec,p1->cou_num);
		fclose(fp1);
	} else {
		printf("\t无该编号的课程！\n");
	}
end:
	;
}

// 课程管理（删除课程）
void delete_cou(int num1) {
	struct course *p1,*p2;
	if(head1==NULL) {
		printf("\n\t没有该课程，无法删除！\n");
		goto end;
	}
	p1=head1;
	while(num1!=p1->cou_num &&p1->next!=NULL) {
		p2=p1;
		p1=p1->next;
	}
	if(num1==p1->cou_num) {
		if(p1==head1)
			head1=p1->next;
		else
			p2->next=p1->next;
		printf("已删除该编号课程！\n");
		FILE *fp1;
		time_t timep;
		struct tm *p;
		time(&timep);
		p=gmtime(&timep);
		fp1=fopen("log.log","a");
		fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t删除了一个课程号为%d课程信息。\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec,num1);
		fclose(fp1);
		N1=N1-1;
	} else
		printf("无该编号的课程！\n");
end:
	;
}


//浏览课程信息
void show_cou(void) {
	struct course *p;
	p=head1;
	printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("课程编号\t课程名称\t课程性质\t授课学时\t学分\t课程已选人数\t课程人数上限\n");
	while(p!=NULL) {
		printf("%6d%14s%18s%12d%16d%12d%16d\n",p->cou_num,p->cou_name,p->cou_type,p->hour,p->score,p->nelepeo,p->melepeo);
		p=p->next;
	}
	FILE *fp1;
	time_t timep;
	struct tm *p1;
	time(&timep);
	p1=gmtime(&timep);
	fp1=fopen("log.log","a");
	fprintf(fp1,"[%d年%d月%d日 %d:%d:%d]\t查看了课程信息。\n",1900+p1->tm_year,1+p1->tm_mon,p1->tm_mday,8+p1->tm_hour,p1->tm_min,p1->tm_sec);
	fclose(fp1);
}

//存储课程信息
void into_cou() {
	FILE *fp;
	struct course *p;
	char filepath[50];
	printf("保存数据路径：\n");
	getchar();
	gets(filepath);
	if((fp=fopen(filepath,"w"))==NULL) {
		printf("无法存储！\n");
		exit(0);
	}
	p=head1;
	while(p!=NULL) {
		fprintf(fp,"%d\t%s\t%s%d\t%d\t%d\t%d\n",p->cou_num,p->cou_name,p->cou_type,p->hour,p->score,p->nelepeo,p->melepeo);
		p = p->next;
	}
	fclose(fp);
	printf("已储存入%s文件！\n",filepath);
}

//课程管理主函数
void manage_cou(void) {
	int i,num1,num2=1;
	print_wel();
	printf("\t课程管理\n");
	printf("\t1. 添加课程\n");
	printf("\t2. 修改课程\n");
	printf("\t3. 删除课程\n");
	printf("\t4. 存储课程信息\n");
	printf("\t5. 浏览课程信息\n");
	printf("\t6. 返回上一级菜单\n");
	printf("\t请选择(1~6): ");
	scanf("%d",&i);
	switch(i) {
		case(1):
			print_wel();
			input_cou();
			break;
		case(2): {
			
			print_wel();
			printf("请输入要修改的课程编号：\n");
			scanf("%d",&num2);
			insert_cou(num2);
			manage_cou();
			
			break;
		}
		case(3): {
			
			print_wel();
			printf("请输入要删除的课程的编号：\n");
			scanf("%d",&num1);
			delete_cou(num1);
			manage_cou();
			
			break;
		}
		case(4):
			
			print_wel();
			into_cou();
			manage_cou();
			break;
		case(5):
			
			print_wel();
			show_cou();
			manage_cou();
			
			break;
		case(6):
			print_wel();
			break;
		default:
			printf("输入有误，请重新输入：\n\n");
			manage_cou(); 
			break;
	}
}
#endif
